package com.delivery.www.service.ServiceImpl;

import com.delivery.www.dao.DaoImpl.CartDaoImpl;
import com.delivery.www.po.Cart;
import com.delivery.www.service.CartService;
import com.delivery.www.dao.CartDao;

import java.util.List;

/**
 * @author 12970
 */
public class CartServiceImpl implements CartService {
    private final CartDao cartDao = new CartDaoImpl();

    @Override
    public boolean addCart(int userId, int storeId) {
        if (userId > 0 && storeId > 0) {
            return cartDao.addCart(userId, storeId) > 0;
        } else {
            return false;
        }
    }

    @Override
    public Cart getCart(int userId, int storeId) {
        if (userId > 0 && storeId > 0) {
            return cartDao.getCart(userId, storeId);
        } else {
            return null;
        }
    }

    @Override
    public List<Cart> getAllCart(int userId) {
        if (userId > 0) {
            return cartDao.getAllCart(userId);
        } else {
            return null;
        }
    }

    @Override
    public Cart judgeCart(int userId, int storeId) {
        Cart cart = getCart(userId, storeId);
        if (cart == null) {
            boolean b = addCart(userId, storeId);
            if (b) {
                cart = getCart(userId, storeId);
                return cart;
            }else {
                return null;
            }
        } else {
            return cart;
        }
    }
}
